﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;                                                                                                                                                                                                                                                                                                                                                                                                                                                   
using System.Windows.Forms;

namespace BooksManager
{ 
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != "" && textBox2.Text != "")
            {
                login();
            }                                                                   
            else
            {
                MessageBox.Show("账户或密码为空，请填写");
            }
        }

        //登录方法，验证允许是否登录，允许返回真
        public void login()
        {
            //用户
            if (radioButtonUser.Checked == true)
            {
                Dao dao = new Dao();
                //string sql1 = "select *  from t_users where id = '"+textBox1.Text+"' and psw = '"+textBox2.Text+"'";
                //string sql2 = String.Format("select * from t_user where id= '{0}' and psw = '{1}'", textBox1.Text, textBox2.Text);
                string sql = $"select * from t_users where id ='{textBox1.Text}' and  psw ='{textBox2.Text}'";
                IDataReader dc = dao.read(sql);

                if( dc.Read())
                //MessageBox.Show(dc[0].ToString() + dc["name"].ToString());
                {
                    Data.UID = dc["id"].ToString();
                    Data.UName = dc["name"].ToString();

                    MessageBox.Show("登录成功");
                    User1 user = new User1();
                    this.Hide();
                    user.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
                dao.DaoClose();
            }

            //管理员
            if(radioButtonAdmin.Checked == true)
            {
                Dao dao = new Dao();
                string sql = $"select * from t_admin where id ='{textBox1.Text}' and  psw ='{textBox2.Text}'";
                //MessageBox.Show(sql3);
                IDataReader dc = dao.read(sql);

                if (dc.Read())
                //MessageBox.Show(dc[0].ToString() + dc["name"].ToString());
                {
                    MessageBox.Show("登录成功");
                    Admin1 admin = new Admin1();
                    this.Hide();
                    admin.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
                dao.DaoClose();
            }
            //MessageBox.Show("请先选择用户类型");
        }
    }
}
